home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Mac / macspeech / macspeechmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-02  |  10.7 KB  |  560 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* xx module */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29.  
  30. #include <GestaltEqu.h>
  31. #include "pascal.h"
  32. #include "Speech.h"
  33.  
  34. /* Somehow the Apple Fix2X and X2Fix don't do what I expect */
  35. #define fixed2double(x) (((double)(x))/32768.0)
  36. #define double2fixed(x) ((Fixed)((x)*32768.0))
  37.  
  38. char *CurrentSpeech;
  39. object *ms_error_object;
  40.  
  41. /* General error handler */
  42. static object *
  43. ms_error(num)
  44.     OSErr num;
  45. {
  46.     char buf[40];
  47.     
  48.     sprintf(buf, "Mac Speech Mgr error #%d", num);
  49.     err_setstr(ms_error_object, buf);
  50.     return (object *)NULL;
  51. }
  52.  
  53. /* -------------
  54. **
  55. ** Part one - the speech channel object
  56. */
  57. typedef struct {
  58.     OB_HEAD
  59.     object    *x_attr;    /* Attributes dictionary */
  60.     SpeechChannel chan;
  61.     object *curtext;    /* If non-NULL current text being spoken */
  62. } scobject;
  63.  
  64. staticforward typeobject sctype;
  65.  
  66. #define is_scobject(v)        ((v)->ob_type == &sctype)
  67.  
  68. static scobject *
  69. newscobject(arg)
  70.     VoiceSpec *arg;
  71. {
  72.     scobject *xp;
  73.     OSErr err;
  74.     
  75.     xp = NEWOBJ(scobject, &sctype);
  76.     if (xp == NULL)
  77.         return NULL;
  78.     xp->x_attr = NULL;
  79.     if ( (err=NewSpeechChannel(arg, &xp->chan)) != 0) {
  80.         DECREF(xp);
  81.         return (scobject *)ms_error(err);
  82.     }
  83.     xp->curtext = NULL;
  84.     return xp;
  85. }
  86.  
  87. /* sc methods */
  88.  
  89. static void
  90. sc_dealloc(xp)
  91.     scobject *xp;
  92. {
  93.     DisposeSpeechChannel(xp->chan);
  94.     XDECREF(xp->x_attr);
  95.     DEL(xp);
  96. }
  97.  
  98. static object *
  99. sc_Stop(self, args)
  100.     scobject *self;
  101.     object *args;
  102. {
  103.     OSErr err;
  104.     
  105.     if (!getnoarg(args))
  106.         return NULL;
  107.     if ((err=StopSpeech(self->chan)) != 0)
  108.         return ms_error(err);
  109.     if ( self->curtext ) {
  110.         DECREF(self->curtext);
  111.         self->curtext = NULL;
  112.     }
  113.     INCREF(None);
  114.     return None;
  115. }
  116.  
  117. static object *
  118. sc_SpeakText(self, args)
  119.     scobject *self;
  120.     object *args;
  121. {
  122.     OSErr err;
  123.     char *str;
  124.     int len;
  125.     
  126.     if (!getargs(args, "s#", &str, &len))
  127.         return NULL;
  128.     if ( self->curtext ) {
  129.         StopSpeech(self->chan);
  130.         DECREF(self->curtext);
  131.         self->curtext = NULL;
  132.     }
  133.     if ((err=SpeakText(self->chan, (Ptr)str, (long)len)) != 0)
  134.         return ms_error(err);
  135.     (void)getargs(args, "O", &self->curtext);    /* Or should I check this? */
  136.     INCREF(self->curtext);
  137.     INCREF(None);
  138.     return None;
  139. }
  140.  
  141. static object *
  142. sc_GetRate(self, args)
  143.     scobject *self;
  144.     object *args;
  145. {
  146.     OSErr err;
  147.     Fixed farg;
  148.     
  149.     if (!getnoarg(args))
  150.         return NULL;
  151.     if ((err=GetSpeechRate(self->chan, &farg)) != 0)
  152.         return ms_error(err);
  153.     return newfloatobject(fixed2double(farg));
  154. }
  155.  
  156. static object *
  157. sc_GetPitch(self, args)
  158.     scobject *self;
  159.     object *args;
  160. {
  161.     OSErr err;
  162.     Fixed farg;
  163.     
  164.     if (!getnoarg(args))
  165.         return NULL;
  166.     if ((err=GetSpeechPitch(self->chan, &farg)) != 0)
  167.         return ms_error(err);
  168.     return newfloatobject(fixed2double(farg));
  169. }
  170.  
  171. static object *
  172. sc_SetRate(self, args)
  173.     scobject *self;
  174.     object *args;
  175. {
  176.     OSErr err;
  177.     double darg;
  178.     
  179.     if (!getargs(args, "d", &darg))
  180.         return NULL;
  181.     if ((err=SetSpeechRate(self->chan, double2fixed(darg))) != 0)
  182.         return ms_error(err);
  183.     INCREF(None);
  184.     return None;
  185. }
  186.  
  187. static object *
  188. sc_SetPitch(self, args)
  189.     scobject *self;
  190.     object *args;
  191. {
  192.     OSErr err;
  193.     double darg;
  194.     
  195.     if (!getargs(args, "d", &darg))
  196.         return NULL;
  197.     if ((err=SetSpeechPitch(self->chan, double2fixed(darg))) != 0)
  198.         return ms_error(err);
  199.     INCREF(None);
  200.     return None;
  201. }
  202.  
  203. static struct methodlist sc_methods[] = {
  204.     {"Stop",        (method)sc_Stop},
  205.     {"SetRate",        (method)sc_SetRate},
  206.     {"GetRate",        (method)sc_GetRate},
  207.     {"SetPitch",    (method)sc_SetPitch},
  208.     {"GetPitch",    (method)sc_GetPitch},
  209.     {"SpeakText",    (method)sc_SpeakText},
  210.     {NULL,            NULL}        /* sentinel */
  211. };
  212.  
  213. static object *
  214. sc_getattr(xp, name)
  215.     scobject *xp;
  216.     char *name;
  217. {
  218.     if (xp->x_attr != NULL) {
  219.         object *v = dictlookup(xp->x_attr, name);
  220.         if (v != NULL) {
  221.             INCREF(v);
  222.             return v;
  223.         }
  224.     }
  225.     return findmethod(sc_methods, (object *)xp, name);
  226. }
  227.  
  228. static int
  229. sc_setattr(xp, name, v)
  230.     scobject *xp;
  231.     char *name;
  232.     object *v;
  233. {
  234.     if (xp->x_attr == NULL) {
  235.         xp->x_attr = newdictobject();
  236.         if (xp->x_attr == NULL)
  237.             return -1;
  238.     }
  239.     if (v == NULL) {
  240.         int rv = dictremove(xp->x_attr, name);
  241.         if (rv < 0)
  242.             err_setstr(AttributeError,
  243.                     "delete non-existing sc attribute");
  244.         return rv;
  245.     }
  246.     else
  247.         return dictinsert(xp->x_attr, name, v);
  248. }
  249.  
  250. static typeobject sctype = {
  251.     OB_HEAD_INIT(&Typetype)
  252.     0,            /*ob_size*/
  253.     "MacSpeechChannel",            /*tp_name*/
  254.     sizeof(scobject),    /*tp_basicsize*/
  255.     0,            /*tp_itemsize*/
  256.     /* methods */
  257.     (destructor)sc_dealloc, /*tp_dealloc*/
  258.     0,            /*tp_print*/
  259.     (getattrfunc)sc_getattr, /*tp_getattr*/
  260.     (setattrfunc)sc_setattr, /*tp_setattr*/
  261.     0,            /*tp_compare*/
  262.     0,            /*tp_repr*/
  263.     0,            /*tp_as_number*/
  264.     0,            /*tp_as_sequence*/
  265.     0,            /*tp_as_mapping*/
  266.     0,            /*tp_hash*/
  267. };
  268.  
  269. /* -------------
  270. **
  271. ** Part two - the voice object
  272. */
  273. typedef struct {
  274.     OB_HEAD
  275.     object    *x_attr;    /* Attributes dictionary */
  276.     int        initialized;
  277.     VoiceSpec    vs;
  278.     VoiceDescription vd;
  279. } mvobject;
  280.  
  281. staticforward typeobject mvtype;
  282.  
  283. #define is_mvobject(v)        ((v)->ob_type == &mvtype)
  284.  
  285. static mvobject *
  286. newmvobject()
  287. {
  288.     mvobject *xp;
  289.     xp = NEWOBJ(mvobject, &mvtype);
  290.     if (xp == NULL)
  291.         return NULL;
  292.     xp->x_attr = NULL;
  293.     xp->initialized = 0;
  294.     return xp;
  295. }
  296.  
  297. static int
  298. initmvobject(self, ind)
  299.     mvobject *self;
  300.     int ind;
  301. {
  302.     OSErr err;
  303.     
  304.     if ( (err=GetIndVoice((short)ind, &self->vs)) != 0 ) {
  305.         ms_error(err);
  306.         return 0;
  307.     }
  308.     if ( (err=GetVoiceDescription(&self->vs, &self->vd, sizeof self->vd)) != 0) {
  309.         ms_error(err);
  310.         return 0;
  311.     }
  312.     self->initialized = 1;
  313.     return 1;
  314. /* mv methods */
  315.  
  316. static void
  317. mv_dealloc(xp)
  318.     mvobject *xp;
  319. {
  320.     XDECREF(xp->x_attr);
  321.     DEL(xp);
  322. }
  323.  
  324. static object *
  325. mv_getgender(self, args)
  326.     mvobject *self;
  327.     object *args;
  328. {
  329.     object *rv;
  330.     
  331.     if (!getnoarg(args))
  332.         return NULL;
  333.     if (!self->initialized) {
  334.         err_setstr(ms_error_object, "Uninitialized voice");
  335.         return NULL;
  336.     }
  337.     rv = newintobject(self->vd.gender);
  338.     return rv;
  339. }
  340.  
  341. static object *
  342. mv_newchannel(self, args)
  343.     mvobject *self;
  344.     object *args;
  345. {    
  346.     if (!getnoarg(args))
  347.         return NULL;
  348.     if (!self->initialized) {
  349.         err_setstr(ms_error_object, "Uninitialized voice");
  350.         return NULL;
  351.     }
  352.     return (object *)newscobject(&self->vs);
  353. }
  354.  
  355. static struct methodlist mv_methods[] = {
  356.     {"GetGender",    (method)mv_getgender},
  357.     {"NewChannel",    (method)mv_newchannel},
  358.     {NULL,        NULL}        /* sentinel */
  359. };
  360.  
  361. static object *
  362. mv_getattr(xp, name)
  363.     mvobject *xp;
  364.     char *name;
  365. {
  366.     if (xp->x_attr != NULL) {
  367.         object *v = dictlookup(xp->x_attr, name);
  368.         if (v != NULL) {
  369.             INCREF(v);
  370.             return v;
  371.         }
  372.     }
  373.     return findmethod(mv_methods, (object *)xp, name);
  374. }
  375.  
  376. static int
  377. mv_setattr(xp, name, v)
  378.     mvobject *xp;
  379.     char *name;
  380.     object *v;
  381. {
  382.     if (xp->x_attr == NULL) {
  383.         xp->x_attr = newdictobject();
  384.         if (xp->x_attr == NULL)
  385.             return -1;
  386.     }
  387.     if (v == NULL) {
  388.         int rv = dictremove(xp->x_attr, name);
  389.         if (rv < 0)
  390.             err_setstr(AttributeError,
  391.                     "delete non-existing MacVoice attribute");
  392.         return rv;
  393.     }
  394.     else
  395.         return dictinsert(xp->x_attr, name, v);
  396. }
  397.  
  398. static typeobject mvtype = {
  399.     OB_HEAD_INIT(&Typetype)
  400.     0,            /*ob_size*/
  401.     "MacVoice",            /*tp_name*/
  402.     sizeof(mvobject),    /*tp_basicsize*/
  403.     0,            /*tp_itemsize*/
  404.     /* methods */
  405.     (destructor)mv_dealloc, /*tp_dealloc*/
  406.     0,            /*tp_print*/
  407.     (getattrfunc)mv_getattr, /*tp_getattr*/
  408.     (setattrfunc)mv_setattr, /*tp_setattr*/
  409.     0,            /*tp_compare*/
  410.     0,            /*tp_repr*/
  411.     0,            /*tp_as_number*/
  412.     0,            /*tp_as_sequence*/
  413.     0,            /*tp_as_mapping*/
  414.     0,            /*tp_hash*/
  415. };
  416.  
  417.  
  418. /* -------------
  419. **
  420. ** Part three - The module interface
  421. */
  422.  
  423. /* See if Speech manager available */
  424.  
  425. static object *
  426. ms_Available(self, args)
  427.     object *self; /* Not used */
  428.     object *args;
  429. {
  430.     OSErr err;
  431.     long result;
  432.     
  433.     if (!getnoarg(args))
  434.         return NULL;
  435.     err = Gestalt(gestaltSpeechAttr, &result);
  436.     if ( err == noErr && (result & (1<<gestaltSpeechMgrPresent)))
  437.         result = 1;
  438.     else
  439.         result = 0;
  440.     return newintobject(result);
  441. }
  442.  
  443. /* Count number of busy speeches */
  444.  
  445. static object *
  446. ms_Busy(self, args)
  447.     object *self; /* Not used */
  448.     object *args;
  449. {
  450.     OSErr err;
  451.     short result;
  452.     
  453.     if (!getnoarg(args))
  454.         return NULL;
  455.     result = SpeechBusy();
  456.     return newintobject(result);
  457. }
  458.  
  459. /* Say something */
  460.  
  461. static object *
  462. ms_SpeakString(self, args)
  463.     object *self; /* Not used */
  464.     object *args;
  465. {
  466.     OSErr err;
  467.     short result;
  468.     char *str;
  469.     int len;
  470.     
  471.     if (!getstrarg(args, &str))
  472.         return NULL;
  473.     if (CurrentSpeech) {
  474.         /* Free the old speech, after killing it off
  475.         ** (note that speach is async and c2pstr works inplace)
  476.         */
  477.         SpeakString("\p");
  478.         free(CurrentSpeech);
  479.     }
  480.     len = strlen(str);
  481.     CurrentSpeech = malloc(len+1);
  482.     strcpy(CurrentSpeech, str);
  483.     err = SpeakString(c2pstr(CurrentSpeech));
  484.     if ( err )
  485.         return ms_error(err);
  486.     INCREF(None);
  487.     return None;
  488. }
  489.  
  490.  
  491. /* Count number of available voices */
  492.  
  493. static object *
  494. ms_CountVoices(self, args)
  495.     object *self; /* Not used */
  496.     object *args;
  497. {
  498.     OSErr err;
  499.     short result;
  500.     
  501.     if (!getnoarg(args))
  502.         return NULL;
  503.     CountVoices(&result);
  504.     return newintobject(result);
  505. }
  506.  
  507. static object *
  508. ms_GetIndVoice(self, args)
  509.     object *self; /* Not used */
  510.     object *args;
  511. {
  512.     OSErr err;
  513.     mvobject *rv;
  514.     long ind;
  515.     
  516.     if( !getargs(args, "i", &ind))
  517.         return 0;
  518.     rv = newmvobject();
  519.     if ( !initmvobject(rv, ind) ) {
  520.         DECREF(rv);
  521.         return NULL;
  522.     }
  523.     return (object *)rv;
  524. }
  525.  
  526.  
  527.  
  528. /* List of functions defined in the module */
  529.  
  530. static struct methodlist ms_methods[] = {
  531.     {"Available",    ms_Available},
  532.     {"CountVoices",    ms_CountVoices},
  533.     {"Busy",        ms_Busy},
  534.     {"SpeakString",    ms_SpeakString},
  535.     {"GetIndVoice", ms_GetIndVoice},
  536.     {NULL,        NULL}        /* sentinel */
  537. };
  538.  
  539.  
  540. /* Initialization function for the module (*must* be called initmacspeech) */
  541.  
  542. void
  543. initmacspeech()
  544. {
  545.     object *m, *d;
  546.  
  547.     /* Create the module and add the functions */
  548.     m = initmodule("macspeech", ms_methods);
  549.  
  550.     /* Add some symbolic constants to the module */
  551.     d = getmoduledict(m);
  552.     ms_error_object = newstringobject("macspeech.error");
  553.     dictinsert(d, "error", ms_error_object);
  554.  
  555.     /* Check for errors */
  556.     if (err_occurred())
  557.         fatal("can't initialize module macspeech");
  558. }
  559.